package com.lotus.lqmp.service.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 切面
 * @author Liwei.Zhang3
 * @since 2022/01/05 13:47
 */
@Slf4j
@Aspect
@Component
public class CostTimeAop {

    @Pointcut("@within(com.lotus.lqmp.service.common.annotations.CostTimeRecord) "
        + "|| @annotation(com.lotus.lqmp.service.common.annotations.CostTimeRecord)")
    public void recordAspect() {}

    @Around("recordAspect()")
    public Object recordAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object[] args = proceedingJoinPoint.getArgs();
        Object ret = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        log.info("type-{} method-{} in-{} out-{} spent-{}ms", methodSignature.getDeclaringType().getSimpleName(),
            methodSignature.getName(), args, ret, end - start);
        return ret;
    }
}
